package kaizone.songmaya.smartns.comm.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SecretUtils {
    public static final String MD5(byte[] md) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

        try {
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;

            for (int i = 0; i < j; ++i) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 15];
                str[k++] = hexDigits[byte0 & 15];
            }
            return new String(str);
        } catch (Exception var7) {
            return null;
        }
    }

    public static String AesEncrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        } else if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        } else {
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(1, skeySpec);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
            return (new Base64()).encodeToString(encrypted);
        }
    }

    public static String AesDecrypt(String sSrc, String sKey) throws Exception {
        try {
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            } else if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            } else {
                byte[] raw = sKey.getBytes("utf-8");
                SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(2, skeySpec);
                byte[] encrypted1 = (new Base64()).decode(sSrc);

                try {
                    byte[] original = cipher.doFinal(encrypted1);
                    String originalString = new String(original, "utf-8");
                    return originalString;
                } catch (Exception var8) {
                    System.out.println(var8.toString());
                    return null;
                }
            }
        } catch (Exception var9) {
            System.out.println(var9.toString());
            return null;
        }
    }

    public static String simpleEncrypt(String sSrc) {
        byte[] bas = sSrc.getBytes();

        int half;
        for (half = 0; half < bas.length; ++half) {
            bas[half] = (byte) (~bas[half]);
        }

        half = bas.length / 2;

        for (int i = 0; i < half; ++i) {
            if (i % 2 == 1) {
                byte b = bas[i];
                bas[i] = bas[i + half];
                bas[i + half] = b;
            }
        }

        return (new Base64()).encodeToString(bas);
    }

    public static String simpleDecrypt(String sSrc) {
        byte[] bas = (new Base64()).decode(sSrc);
        int half = bas.length / 2;

        int i;
        for (i = 0; i < half; ++i) {
            if (i % 2 == 1) {
                byte b = bas[i];
                bas[i] = bas[i + half];
                bas[i + half] = b;
            }
        }

        for (i = 0; i < bas.length; ++i) {
            bas[i] = (byte) (~bas[i]);
        }

        return new String(bas);
    }
}
